﻿using ParboiledSharp.parboiled.dataobjects;

namespace ParboiledSharp.parboiled.rules
{
    class OneOrMore : ContainerRule
    {
        private int _lastIterationsCount = 0;

        public OneOrMore(object rule)
        {
            SetRule(rule);
        }

        public OneOrMore()
        {
        }

        internal override bool Execute(Context context)
        {
            _lastIterationsCount = 0;
            int count = 0;
            while (true)
            {
                bool result = ExecuteRule(GetRule(), context);
                if (result)
                {
                    count++;
                }
                else
                {
                    break;
                }
            }
            _lastIterationsCount = count;
            return count > 0;
        }

        public override void EndRule(bool result, Context context) {
            if (result)
            {
                context.SetlastIterationsCount(_lastIterationsCount);
            }
        }
    }
}
